package visitor;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Matt
 */
import exprtree.*;

public class CalculateDoubles implements Visitor {

	private double x;
	
    public Object visit(DoubleLitExpr e){
       return new Double(e.getLiteral());
    }
    
    public Object visit(VarExpr e){
    	return new Double((Double)x);
    }
    
    public Object visit(AddExpr e){
        return new Double ( ((Double)e.getLeftExpr().accept(this)).doubleValue()
                + ((Double)e.getRightExpr().accept(this)).doubleValue());

    }
    public Object visit(MinusExpr e){
        return new Double ( ((Double)e.getLeftExpr().accept(this)).doubleValue()
                - ((Double)e.getRightExpr().accept(this)).doubleValue());
    }
    public Object visit(MultExpr e){
        return new Double ( ((Double)e.getLeftExpr().accept(this)).doubleValue()
                * ((Double)e.getRightExpr().accept(this)).doubleValue());

    }
    public Object visit(DivExpr e){
        return new Double ( ((Double)e.getLeftExpr().accept(this)).doubleValue()
                / ((Double)e.getRightExpr().accept(this)).doubleValue());

    }
    public Object visit(PowExpr e){
    	Double left = ((Double)e.getExpr().accept(this)).doubleValue();
    	Double right = ((Double)e.getPower().accept(this)).doubleValue();
    	Double answer = Math.pow(left, right);
        return answer;

    }
    public Object visit(SqrtExpr e){
        return new Double (Math.sqrt(((Double)e.getExpr().accept(this)).doubleValue()));
    }
    public Object visit(CosExpr e){
        return new Double (Math.cos(((Double)e.getExpr().accept(this)).doubleValue()));
    }
    public Object visit(SinExpr e){
        return new Double (Math.sin(((Double)e.getExpr().accept(this)).doubleValue()));
    }
    public Object visit(TanExpr e){
        return new Double (Math.tan(((Double)e.getExpr().accept(this)).doubleValue()));
    }
    public Object visit(UnaryMinusExpr e){
    	return -((Double) (e.getExpr().accept(this)));
    }
	@Override
	public Object visit(Expr e) {
		if (e instanceof AddExpr){
            return visit((AddExpr)(e));
        }else if(e instanceof MinusExpr){
            return visit((MinusExpr)(e));
        }else if(e instanceof MultExpr){
            return visit((MultExpr)(e));
        }else if(e instanceof DivExpr){
            return visit((DivExpr)(e));
        }else if(e instanceof SqrtExpr){
            return visit((SqrtExpr)(e));
        }else if(e instanceof SinExpr){
            return visit((SinExpr)(e));
        }else if(e instanceof CosExpr){
            return visit((CosExpr)(e));
        }else if(e instanceof TanExpr){
            return visit((TanExpr)(e));
        }else if(e instanceof DoubleLitExpr){
        	return visit((DoubleLitExpr) (e));
        }else if(e instanceof VarExpr){
        	return visit((VarExpr) (e));
        }else if(e instanceof PowExpr){
        	return visit((PowExpr) (e));
        }else if(e instanceof UnaryMinusExpr){
        	return visit((UnaryMinusExpr) (e));
        }else {
        	return null;
        }
	}

	public void setX(double x) {
		this.x = x;
	}

	public double getX() {
		return x;
	}
}

